{% load display_tags %}
{% load authorization_tags %}
{% if surveys %}
    <table id="surveys" class="tablesorter-bootstrap table table-condensed table-striped">
        <thead>
        <tr>
            <th></th>
            <th>Name</th>
            <th>Assignee</th>
            <th>Responder</th>
            <th>Status</th>
            <th>Completion Date</th>
        </tr>
        </thead>
        {% for survey in surveys %}
            <tr>
                <td>
                    <ul>
                        <li class="dropdown" style="list-style:none;position:absolute">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="true">&nbsp;<b class="fa-solid fa-ellipsis-vertical"></b>&nbsp;</a>
                            <ul class="dropdown-menu">
                                {% if not survey.completed %}
                                <li role="presentation">
                                    <a class="" href="/engagement/{{ survey.engagement.id }}/questionnaire/{{ survey.id }}/answer">
                                    <i class="fa-solid fa-pen-to-square"></i> Answer Questionnaire</a>
                                </li>
                                {% else %}
                                <li role="presentation">
                                    <a class="" href="/engagement/{{ survey.engagement.id }}/questionnaire/{{ survey.id }}">
                                        <i class="fa-solid fa-rectangle-list"></i> View Responses</a>
                                </li>
                                <li role="presentation">
                                    <a class="" href="/engagement/{{ survey.engagement.id }}/questionnaire/{{ survey.id }}/answer">
                                    <i class="fa-solid fa-pen-to-square"></i> Edit Responses</a>
                                </li>
                                {% endif %}
                                {% if survey.engagement|has_object_permission:"Engagement_Edit" %}
                                <li role="presentation"
                                    ><a class="" href="/engagement/{{ survey.engagement.id }}/questionnaire/{{ survey.id }}/assign">
                                        <i class="fa-solid fa-plus"></i> Assign User</a>
                                </li>
                                {% endif %}
                                {% if survey.engagement|has_object_permission:"Engagement_Edit" %}
                                <li role="presentation">
                                    <a class="" href="/empty_questionnaire/{{ survey.id }}/existing_engagement">
                                        <i class="fa-solid fa-link"></i> Link to a Different Engagement
                                    </a>
                                </li>
                                {% endif %}
                                <li>
                                    <a class="" data-toggle="modal"
                                    data-target="#shareQuestionnaireModal"
                                    data-whatever="/engagement/{{ survey.engagement.id }}/questionnaire/{{ survey.id }}/answer">
                                    <i class="fa-solid fa-share"></i> Share Questionnaire</a>
                                </li>
                                {% if survey.engagement|has_object_permission:"Engagement_Edit" %}
                                <li class="divider"></li>
                                <li role="presentation">
                                    <a class="text-danger" href="/engagement/{{ survey.engagement.id }}/questionnaire/{{ survey.id }}/delete">
                                    <i class="fa-solid fa-trash"></i> Delete Questionnaire</a>
                                </li>
                                {% endif %}
                            </ul>
                        </li>
                    </ul>
                </td>
                <td><a href="/engagement/{{ survey.engagement.id }}/questionnaire/{{ survey.id }}">{{ survey.survey.name }}</a></td>  
                <td>
                    {% if survey.assignee == None %}
                        Not Assigned
                    {% else %}
                        {{ survey.assignee.username }} 
                    {% endif %}
                </td>
                <td>
                    {% if survey.responder %}
                        {{ survey.responder }}
                    {% else %}
                        Anonymous
                    {% endif %}
                </td>
                <td>
                    {% if survey.completed %}
                        <span class="pass_fail Pass">Completed</span>
                    {% else %}
                        <span class="pass_fail">Unanswered</span>
                    {% endif %}
                </td>
                <td>{{ survey.answered_on }}</td>
            </tr>
        {% endfor %}
    </table>
{% else %}
    <div class="panel-body">
        <p>No Questionnaires found.</p>
    </div>
{% endif %}